from odoo import http, fields
from odoo.http import request
from ...tools.decorator import validate_rp_api, exception_handler
from ..abstract_interface.login_and_register import APILoginAndRegister
from ...tools.encapsulation import valid_response, invalid_business, invalid_response
from ...static import request_schema_json


class APILoginAndRegisterImplement(APILoginAndRegister):
    """
    管理登陆注册类接口
    """

    @exception_handler
    @http.route(route='/api/login/web_guest_login', auth='none', methods=['POST'], cors="*", csrf=False)
    def web_guest_login(self, **kw):
        """ web端游客登陆 """
        username = 'admin_for_web_guest'
        password = 'admin123'
        _token = request.env["api.access.token"]
        # 获取用户
        user_rcd = request.env['rp.customer'].sudo().search([('username', '=', username), ('password', '=', password)])
        # 获取用户token
        access_token = _token.find_one_or_create_token(customer_id=user_rcd.id, create=True)
        data = {'token': access_token}
        return valid_response(data)

    @exception_handler
    @validate_rp_api(schema=request_schema_json.web_user_login)
    @http.route(route='/api/login/web_user_login', auth='none', methods=['POST'], cors="*", csrf=False)
    def web_user_login(self, **kw):
        """ web端会员登陆 """
        username = kw.get('username')
        password = kw.get('password')
        return valid_response({'aa': '登陆成功'})
